package database;

import java.util.ArrayList;
import java.util.List;

public class HasSongTable extends Table {

	private static final String HAS_SONG_INSERT = "INSERT INTO HasSong VALUES (%s, '%s')";
	private static final String HAS_SONG_DELETE = "DELETE FROM HasSong WHERE upc=%s AND title='%s'";
	private static final String HAS_SONG_DISPLAY = "SELECT * FROM HasSong";	
	
	private static final String HAS_SONG_EXISTS_QUERY = "SELECT * FROM HasSong WHERE upc='%s' AND title='%s'";
	
	public enum Column
	{
		UPC(1, true),
		TITLE(2, false);
				
		private int index;
		private boolean isPrimaryKey;
		
		private String[] columnNames = {
				"upc",
				"title"};
		
		private Column(int index, boolean isPrimaryKey) 
		{
			this.index = index;
			this.isPrimaryKey = isPrimaryKey;
		}
		
		public int getIndex()
		{
			return this.index;
		}
		
		public String toString()
		{
			return columnNames[this.ordinal()];
		}

		public boolean isPrimaryKey() {
			return this.isPrimaryKey;
		}
	};
	
	public HasSongTable()
	{
		super(HAS_SONG_INSERT, HAS_SONG_DELETE, HAS_SONG_DISPLAY, HAS_SONG_EXISTS_QUERY);
	}

	@Override
	public List<String> getPrimaryKeyValues(List<String> insertValues) {
		List<String> primaryKeyValues = new ArrayList<String>();
		for(Column col : Column.values())
		{
			if(col.isPrimaryKey())
			{	
				primaryKeyValues.add(insertValues.get(col.ordinal()));
			}
		}				
		
		return primaryKeyValues;
	}

	@Override
	public List<String> getColumnNames() {
		Column columns[] = Column.values();
		
		List<String> columnNames = new ArrayList<String>(columns.length);
		for( Column col : columns)
		{
			columnNames.add(col.ordinal(), col.toString());
		}
		return columnNames;
	}

	@Override
	public List<String> getPrimaryKeyColumnNames() {
		Column columns[] = Column.values();
		
		List<String> primaryKeyColumnNames = new ArrayList<String>(columns.length);
		for( Column col : columns)
		{
			if(col.isPrimaryKey())
			{
				primaryKeyColumnNames.add(col.ordinal(), col.toString());
			}
		}
		return primaryKeyColumnNames;
	}
	
}
